package com.yss.kettle.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.common.core.utils.uuid.IdUtils;
import com.yss.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.kettle.domain.JobRecord;
import com.yss.kettle.service.IJobRecordService;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;

/**
 * 日志信息Controller
 *
 * @author yss
 * @date 2023-04-25
 */
@RestController
@RequestMapping("/record")
public class JobRecordController extends BaseController {
    @Autowired
    private IJobRecordService jobRecordService;

    /**
     * 查询日志信息列表
     */
    @RequiresPermissions("kettle:record:list")
    @GetMapping("/list")
    public TableDataInfo list(JobRecord jobRecord) {
        startPage();
        List<JobRecord> list = jobRecordService.selectJobRecordList(jobRecord);
        return getDataTable(list);
    }

    /**
     * 导出日志信息列表
     */
    @RequiresPermissions("kettle:record:export")
    @Log(title = "日志信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JobRecord jobRecord) {
        List<JobRecord> list = jobRecordService.selectJobRecordList(jobRecord);
        EasyExeclUtil.export(response, list, JobRecord.class);
    }

    /**
     * 获取日志信息详细信息
     */
    @RequiresPermissions("kettle:record:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(jobRecordService.getById(id));
    }

    /**
     * 新增日志信息
     */
    @RequiresPermissions("kettle:record:add")
    @Log(title = "日志信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated JobRecord jobRecord) {
        jobRecord.setId(IdUtils.fastSimpleUUID());
        jobRecord.setCreateBy(SecurityUtils.getUsername());
        jobRecord.setCreateTime(new Date());
        return toAjax(jobRecordService.save(jobRecord));
    }

    /**
     * 修改日志信息
     */
    @RequiresPermissions("kettle:record:edit")
    @Log(title = "日志信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated JobRecord jobRecord) {
        jobRecord.setUpdateTime(new Date());
        jobRecord.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(jobRecordService.updateById(jobRecord));
    }

    /**
     * 删除日志信息
     */
    @RequiresPermissions("kettle:record:remove")
    @Log(title = "日志信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        return toAjax(jobRecordService.removeByIds(ids));
    }

    /**
     * 清空日志信息
     */
    @RequiresPermissions("kettle:record:clean")
    @Log(title = "日志信息", businessType = BusinessType.DELETE)
    @GetMapping("/handleClean")
    public AjaxResult handleClean() {
        return toAjax(jobRecordService.remove(new QueryWrapper<>()));
    }
}
